﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Adaptive_Clustering
{
    public class ResultImage
    {
        double[] Sarray;
        int kCounter;
        double[] CountSilhouettePerRegion;

        double TotalSilhouette = new double();
        Byte[,] ImageArray, ImageClusterIndexArray;

        public ResultImage() {  }
       
        public Byte[,] GetImageArray() { return ImageArray; }
       
        public Byte[,] GetImageClusterIndexArray() { return ImageClusterIndexArray; }
        
        public double GetTotalSx() { return TotalSilhouette; }

        public double GetBestSxIndexValue()
        {
            return Sarray[GetBestSxIndex()];
        }

        public int GetBestSxIndex()
        {
            int MaxSxIndex = 0;
            double MaxSx = Sarray[0];   
                for (int l = 1; l < kCounter; l++)
                    if ((MaxSx < Sarray[l]) && (CountSilhouettePerRegion[l] > (ImageArray.GetLength(0) / 5)))
                    {
                        MaxSx = Sarray[l];
                        MaxSxIndex = l;
                    }
            return MaxSxIndex; 
        }

        public void SaveImage(Byte[,] ImageArray, Byte[,] ImageClusterIndexArray,int kCounter)
        {
            this.kCounter = kCounter;
            this.ImageArray = ImageArray;
            this.ImageClusterIndexArray = ImageClusterIndexArray;
            Sarray = RunSilhouette(ImageArray, ImageClusterIndexArray, kCounter);
        }

        public double[] RunSilhouette(Byte[,] ImageArray, Byte[,] ImageClusterIndexArray, int k)
        {
            int i; 
            double TotalCountSilhouette = new double();
            double[] SumSilhouettePerRegion = new double[k];
            CountSilhouettePerRegion = new double[k];
            double[][] SumSilhouetteLineOfPixels = new double[ImageArray.GetLength(1)][];
            double[][] CountSilhouetteLineOfPixels = new double[ImageArray.GetLength(1)][];
            for (i = 0; i < ImageArray.GetLength(0); i++) //add maximum distance into its region and count
            {
                Parallel.For(0, ImageArray.GetLength(1), j =>
                {
                    Silhouette sltt = new Silhouette(k);
                    sltt.silhouette(ImageClusterIndexArray, i, j, k);
                    SumSilhouetteLineOfPixels[j] = sltt.GetSum();
                    CountSilhouetteLineOfPixels[j] = sltt.GetCount();
                });
                for (int l = 0; l < ImageArray.GetLength(1); l++)
                    for (int m = 0; m < k; m++)
                    {
                        SumSilhouettePerRegion[m] += SumSilhouetteLineOfPixels[l][m];
                        CountSilhouettePerRegion[m] += CountSilhouetteLineOfPixels[l][m];
                    }
            }

            for (int l = 0; l < k; l++) // calc AVG of region and Find AVG for all regions
            {
                if (CountSilhouettePerRegion[l] > (ImageArray.GetLength(0) / 5))
                {
                    TotalSilhouette += SumSilhouettePerRegion[l];
                    TotalCountSilhouette += CountSilhouettePerRegion[l];
                }
                SumSilhouettePerRegion[l] = SumSilhouettePerRegion[l] / CountSilhouettePerRegion[l];
            }
            TotalSilhouette = TotalSilhouette / TotalCountSilhouette;

            return SumSilhouettePerRegion;
        }

    }

}
